package com.hibernate.daoimp.product;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hibernate.dao.product.ProductLeibieServiceDao;
import com.hibernate.pojo.ArticlesLeibie;
import com.hibernate.pojo.Product;
import com.hibernate.pojo.ProductAtrribute;
import com.hibernate.pojo.ProductLeibie;
import com.struts.form.ArticlesLeibieForm;
import com.struts.form.ProductLeibieForm;

public class ProductLeibieServiceDaoImp extends HibernateDaoSupport implements ProductLeibieServiceDao
{
	private boolean bool;
	private List list;
	
	public boolean saveProductLeibie(ProductLeibie productLeibie){
		bool=false;
		try
		{
			this.getHibernateTemplate().save(productLeibie);
			bool=true;
		}
		catch(Exception e)
		{
			bool=false;
			e.printStackTrace();
		}
		return bool;
	}

	
	
	public boolean updateProductLeibie(ProductLeibieForm productLeibieform){
		try
		{
			ProductLeibie productLeibie=(ProductLeibie)this.getHibernateTemplate().load(ProductLeibie.class, productLeibieform.getId());
			productLeibie.setLeibie(productLeibieform.getLeibie());
			this.getHibernateTemplate().update(productLeibie);
			bool=true;
		}
		catch(Exception e)
		{
			bool=false;
			e.printStackTrace();
		}
		return bool;
	}
	
	/**
	 * delete article leibie
	 */
	public boolean deleteProductLeibie(Long id){
		try
		{
			//init List
			list=new ArrayList();
			//load will delete type
			ProductLeibie productLeibie=(ProductLeibie)this.getHibernateTemplate().load(ProductLeibie.class, id);
			list.add(productLeibie);
			//delete related articles
			final Long proleibie=productLeibie.getId();
			//delete all attribute 
			this.getHibernateTemplate().execute(new HibernateCallback()
			{
				public Object doInHibernate(Session session)throws HibernateException, SQLException
				{
					Query query=session.createQuery("from Product where categry_id=:leibie");
					query.setLong("leibie",proleibie);
					List list=query.list();
					for(int i=0;i<list.size();i++)
					{
						Product product=(Product)list.get(i);
						Query query1=session.createQuery("delete from ProductAndProductAtrribute where product_id=:product_id");
						query1.setLong("product_id",product.getId());
						int n=query1.executeUpdate();
					}
					return null;
				}
			});
			//delete all product
			this.getHibernateTemplate().execute(new HibernateCallback()
			{
				public Object doInHibernate(Session session)throws HibernateException, SQLException
				{
					Query query=session.createQuery("delete from Product where categry_id=:leibie");
					query.setLong("leibie",proleibie);
					int n=query.executeUpdate();
					return n;
				}
			});
			//call recursion delete
			List collection=deleteRecursion(id);
			//delete type and child type
			this.getHibernateTemplate().deleteAll(collection);
			bool=true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			bool=false;
		}
		return bool;
	}
	
	public List findProductLeibie(String hql){
		list=this.getHibernateTemplate().find(hql);
		return list;
	}
	
	
	/**
	 * Recursion delete child type , use stack
	 * @param id
	 * @param link
	 */
	public List deleteRecursion(Long id)
	{
		ProductLeibie productleibie=null;
		List collection=new ArrayList();
		collection=this.getHibernateTemplate().find("from ProductLeibie where fatherId="+id);
		if(collection.size()>0)
		{
			for(int i=0;i<collection.size();i++)
			{
				productleibie=(ProductLeibie)collection.get(i);
				list.add(productleibie);
				//delete related product
				final Long proleibie=productleibie.getId();
				
				//delete all attribute 
				this.getHibernateTemplate().execute(new HibernateCallback()
				{
					public Object doInHibernate(Session session)throws HibernateException, SQLException
					{
						Query query=session.createQuery("from Product where categry_id=:leibie");
						query.setLong("leibie",proleibie);
						List list=query.list();
						for(int i=0;i<list.size();i++)
						{
							Product product=(Product)list.get(i);
							Query query1=session.createQuery("delete from ProductAndProductAtrribute where product_id=:product_id");
							query1.setLong("product_id",product.getId());
							int n=query1.executeUpdate();
						}
						return null;
					}
				});
				//delete all product
				this.getHibernateTemplate().execute(new HibernateCallback()
				{
					public Object doInHibernate(Session session)throws HibernateException, SQLException
					{
						Query query=session.createQuery("delete from Product where categry_id=:leibie");
						query.setLong("leibie",proleibie);
						int n=query.executeUpdate();
						return n;
					}
				});
				//call recursion
				deleteRecursion(productleibie.getId());
			}
				
		}
		return list;
	}
}
